home *** CD-ROM | disk | FTP | other *** search
- #!/usr/bin/perl
- #change the line above to the location of the perl interpreter on your system
-
- # rip
- #
- # Author Michel G Delory
- # Created April 1997
- # Last Rev May 1 1997
- #
- # resolve numerical IP addresses in a given log file
- # usage:
- # rip [-f] input_log_file [output_log_file]
- # if [output log file] is omitted, the changes will be made
- # in place, in the original file
- # the -f option is used to force resolution of previously unresolved addresses
- # otherwise rip will only attempt to resolve numerical IP's once
- # resolved IP's are stored in a flat file called rip.db
- #
-
- $usage = "rip\nusage: rip [-f] input_log_file [output_log_file]";
- $usage .= "\n\t-f force resolver on previously unresolved addresses\n";
- $usage .= "\toutput_log_file can be omitted for in place resolving\n";
- $inPlace = 0;
-
- $arg1 = shift @ARGV || die $usage;
- if ($arg1 eq "-f")
- {
- $forceRes = 1;
- $inFile = shift @ARGV || die $usage;
- }
- else
- {
- $inFile = $arg1;
- $forceRes = 0;
- }
- if (!defined ($outFile = shift @ARGV))
- {
- $outFile = "temp.log";
- $inPlace = 1;
- }
-
- if (!open (IN, "$inFile"))
- {
- die "rip\nCan't open input log file $inFile\n$!\n";
- }
-
- if (!open (OUT, ">$outFile"))
- {
- close (IN);
- die "rip\nCan't open/create output log file $outFile\n$!\n";
- }
- chmod 0666, $outFile;
-
- if (!open (DB, "rip.db"))
- {
- %resolvedHost = ();
- }
- else
- {
- while ($line = <DB>)
- {
- ($ip, $host) = split (/\s+/, $line);
- if ($ip ne $host || $forceRes == 0)
- {
- # only include addresses that are really resolved,
- # unless the force resolve flag is not set
- $resolvedHost{$ip} = $host;
- }
- }
- close (DB);
- }
-
- print "rip: analysing $inFile\n";
- logline:
- while ($line = <IN>)
- {
- if ($line =~ /^(\S*)/)
- {
-
- if (int($1) != 0)
- {
- if ($resolved = ($resolvedHost{$1}))
- {
- # this address already in resolved list
- $line = $resolved.$';
- }
- else
- {
- # try to resolve it once
- if ($host = do ResolveIP ($1))
- {
- $resolvedHost{$1} = $host;
- $line = $host.$';
- print "rip: $1 -> $host\n";
- }
- else
- {
- # insert unresolved IP in resolved list
- $resolvedHost{$1} = $1;
- }
-
- }
-
- }
-
- print OUT $line;
-
- }
-
- }
-
-
- close (IN);
- close (OUT);
-
- if (!open (DB, ">rip.db"))
- {
- die "rip\nCan't save resolved hosts in rip.db\n$!\n";
- }
- else
- {
- chmod 0666, "rip.db";
-
- foreach $key (keys (%resolvedHost))
- {
- print DB $key, " ", $resolvedHost{$key}, "\n";
- }
- close (DB);
- }
-
- if ($inPlace == 1)
- {
- if (rename ($outFile, $inFile) == 0)
- {
- die "rip\nCan't overwrite $inFile\n$!\nResolved Log is now stored in the file $outFile\n";
- }
- }
-
-
- sub ResolveIP
- {
- local ($ip) = @_;
- local ($addNum);
-
- $AF_INET = 2;
-
- $addNum = pack ('C4', split (/\./, $ip));
- (gethostbyaddr ($addNum, $AF_INET))[0];
- }
-
-
-
-
-